﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Diagnostics;

namespace DailyFx.AI
{

	public class RuledPlan : IPlan
	{
		/// <summary>
		///		title string of this plan .
		/// </summary>
		private String _title ;

		/// <summary>
		///		Contains all tasks required by this plan, keeping in order !!
		/// </summary>
		private List<RuledTask> _tasks = new List<RuledTask>();
		

		public RuledPlan (String title )
		{
			this._title = title ;
		}

		public String Title 
		{
			get { return this._title; }
		}


		public void addTask ( RuledTask task )
		{
			Debug.Assert ( task != null );

			if ( this._tasks.First( x => x.Label == task.Label ) != null )
				throw new ArgumentException ( "a task named " + task.Label + " already exists in this plan");

			this._tasks.Add ( task);
		}


		public void delTask ( RuledTask task )
		{
			Debug.Assert(task != null);

			if ( !this._tasks.Remove(task)) 
				throw new ArgumentException ( task.ToString() + " is not part of this plan");
		}


		public IReadOnlyList<ITask> estimatePossiblyTasks ( IBlackboard blackboard ) 
		{
			List<ITask> possiblyTasks = new List< ITask>();

			foreach ( var t in this._tasks)
				if ( t.IfAwaked (blackboard) )
					possiblyTasks.Add ( t );

			if ( possiblyTasks.Count == 0  )
				possiblyTasks.Add(NonTask.Singleton);

			return possiblyTasks ;
		}
	}

}
